<?php
namespace app\reportExport\controller;
use app\common\Base;
use app\common\Excel;
use think\Db;

class TeacherReplaceCourseApproval extends Base {
	
	public function index() {
		return $this->fetch();
	}

	public function pageQuery() {
		$page = input('page');
		$rows = input('rows');
		$startDate = input("startDate");
		$endDate = input("endDate");
		$status = input("status");
		$where = [];
		if($status) $where["STATUS"] = array("eq",$status);
		if($startDate && $endDate) {
			$where["CREATE_TIME"] = array("between",[$startDate,$endDate]);
		}else {
			if($startDate) $where["CREATE_TIME"] = array("egt", $startDate);
			if($endDate) $where["CREATE_TIME"] = array("elt", $endDate);
		}
		$result = Db::table("t_teacher_exchange_course_apply_item req")
				  ->join("t_teacher_exchange_course_apply apply","apply.ID = req.APPLY_ID","INNER")
				  ->field("req.ID,req.APPLY_ID,req.OPTION_TYPE,req.SUBJECT_ID,req.SUBJECT_NAME,req.STUDY_TYPE,req.LESSON,req.GRADE,req.CLAZZ,req.COURSE_DATE,req.STATUS,req.REJECT_REASON,req.HAS_EXCHANGE_TEACHER,req.EXCHANGE_TEACHER_ID,req.EXCHANGE_TEACHER_NAME,req.EXCHANGE_COURSE_DATE,req.EXCHANGE_LESSON,req.EXCHANGE_SUBJECT_ID,req.EXCHANGE_SUBJECT_NAME,apply.APPLY_TEACHER_ID,apply.APPLY_TEACHER_NAME,apply.REASON,apply.STATUS APPLY_STATUS,apply.APPROVER_NAME,apply.APPROVER_ID,apply.CREATE_TIME,apply.UPDATE_TIME")
				  ->where($where)
				  ->page($page, $rows)
				  ->order("CREATE_TIME desc,UPDATE_TIME desc")
				  ->select();
		$data = [];
		foreach ($result as $key => $obj) {
			$obj["GRADE_CLAZZ"] = getGradeName($obj["GRADE"],$obj["STUDY_TYPE"]).$obj["CLAZZ"]."班";
			$optionType = $obj["OPTION_TYPE"];
			if(1 == $optionType) $obj["OPTION_TYPE_TEXT"] = "调课";
			else if(2 == $optionType) $obj["OPTION_TYPE_TEXT"] = "代自习";
			else if(3 == $optionType) $obj["OPTION_TYPE_TEXT"] = "看课";
            $status = $obj["STATUS"];
            if("000" == $status) $obj["STATUS_TEXT"] = "教务处审核通过";
            else if("001" == $status) $obj["STATUS_TEXT"] = "待审批";
            else if("002" == $status) $obj["STATUS_TEXT"] = "调代看课老师同意";
            else if("003" == $status) $obj["STATUS_TEXT"] = "调代看老师拒绝";
            else if("004" == $status) $obj["STATUS_TEXT"] = "待教务处审核中";
            else if("005" == $status) $obj["STATUS_TEXT"] = "教务处审核驳回";
            else if("006" == $status) $obj["STATUS_TEXT"] = "教务处撤消";
			$data[$key] = $obj;
		}
		$count = Db::table("t_teacher_exchange_course_apply_item req")
				  ->join("t_teacher_exchange_course_apply apply","apply.ID = req.APPLY_ID","INNER")
				  ->where($where)
				  ->count();
		$grid["rows"] = $data;
		$grid["total"] = $count;
		return $grid;
	}

	public function exportExcel() {
		$startDate = input("startDate");
		$endDate = input("endDate");
		$status = input("status");
		$where = [];
		if($status) $where["STATUS"] = array("eq",$status);
		if($startDate && $endDate) {
			$where["CREATE_TIME"] = array("between",[$startDate,$endDate]);
		}else {
			if($startDate) $where["CREATE_TIME"] = array("egt", $startDate);
			if($endDate) $where["CREATE_TIME"] = array("elt", $endDate);
		}
		$result = Db::table("t_teacher_exchange_course_apply_item req")
				  ->join("t_teacher_exchange_course_apply apply","apply.ID = req.APPLY_ID","INNER")
				  ->field("req.ID,req.APPLY_ID,req.OPTION_TYPE,req.SUBJECT_ID,req.SUBJECT_NAME,req.STUDY_TYPE,req.LESSON,req.GRADE,req.CLAZZ,req.COURSE_DATE,req.STATUS,req.REJECT_REASON,req.HAS_EXCHANGE_TEACHER,req.EXCHANGE_TEACHER_ID,req.EXCHANGE_TEACHER_NAME,req.EXCHANGE_COURSE_DATE,req.EXCHANGE_LESSON,req.EXCHANGE_SUBJECT_ID,req.EXCHANGE_SUBJECT_NAME,apply.APPLY_TEACHER_ID,apply.APPLY_TEACHER_NAME,apply.REASON,apply.STATUS APPLY_STATUS,apply.APPROVER_NAME,apply.APPROVER_ID,apply.CREATE_TIME,apply.UPDATE_TIME")
				  ->where($where)
				  ->order("CREATE_TIME desc,UPDATE_TIME desc")
				  ->select();
		$data = [];
		foreach ($result as $key => $obj) {
			$obj["GRADE_CLAZZ"] = getGradeName($obj["GRADE"],$obj["STUDY_TYPE"]).$obj["CLAZZ"]."班";
			$optionType = $obj["OPTION_TYPE"];
			if(1 == $optionType) $obj["OPTION_TYPE_TEXT"] = "调课";
			else if(2 == $optionType) $obj["OPTION_TYPE_TEXT"] = "代自习";
			else if(3 == $optionType) $obj["OPTION_TYPE_TEXT"] = "看课";
            $status = $obj["STATUS"];
            if("000" == $status) $obj["STATUS_TEXT"] = "教务处审核通过";
            else if("001" == $status) $obj["STATUS_TEXT"] = "待审批";
            else if("002" == $status) $obj["STATUS_TEXT"] = "调代看课老师同意";
            else if("003" == $status) $obj["STATUS_TEXT"] = "调代看老师拒绝";
            else if("004" == $status) $obj["STATUS_TEXT"] = "待教务处审核中";
            else if("005" == $status) $obj["STATUS_TEXT"] = "教务处审核驳回";
            else if("006" == $status) $obj["STATUS_TEXT"] = "教务处撤消";
			$data[$key] = $obj;
		}
		//excel表头内容
		$header = array("申请老师"=>"APPLY_TEACHER_NAME","班级"=>"GRADE_CLAZZ","调代看课类型"=>"OPTION_TYPE_TEXT","申请日期节次"=>"LESSON","调代看课教师"=>"EXCHANGE_TEACHER_NAME","调课课程"=>"EXCHANGE_SUBJECT_NAME","调课日期节次"=>"EXCHANGE_LESSON","审批状态"=>"STATUS_TEXT","审批人"=>"APPROVER_NAME","申请时间"=>"CREATE_TIME");
		Excel::toExcel($data,'调代看课申请表',$header);
		exit;
	}

}
